<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      label-width="auto"
    >
      <el-row>
        <el-form-item label="日期" style="width: 308px">
          <el-date-picker
            v-model="dateRange"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="机台" prop="machineName">
          <el-input
            v-model.trim="queryParams.machineName"
            style="width: 180px"
            placeholder="请输入机台"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="上报人" prop="reportPeople">
          <el-input
            v-model.trim="queryParams.reportPeople"
            style="width: 180px"
            placeholder="请输入上报人"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="车间" prop="workshopName">
          <el-select
            style="width: 180px"
            @change="handleQuery"
            v-model="queryParams.workshopName"
          >
            <el-option label="一车间" value="一车间"></el-option>
            <el-option label="二车间" value="二车间"></el-option>
            <el-option label="三车间" value="三车间"></el-option>
            <el-option label="四车间" value="四车间"></el-option>
            <el-option label="五车间" value="五车间"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-row>
    </el-form>

    <div>
      <el-table
        :summary-method="getSummaries"
        show-summary
        height="65vh"
        v-loading="loading"
        border
        :data="dataList"
      >
        <el-table-column label="序号" width="60" type="index" align="center" />

        <el-table-column
          label="车间名称"
          width="100"
          align="center"
          prop="workshopName"
        ></el-table-column>
        <el-table-column
          label="机台"
          min-width="160"
          align="center"
          prop="machineName"
        ></el-table-column>
        <el-table-column
          label="上报人"
          width="100"
          align="center"
          prop="reportPeople"
        ></el-table-column>
        <el-table-column
          label="开始时间"
          min-width="120"
          align="center"
          prop="beginTime"
        >
          <template v-slot="{ row }">
            {{ parseTime(row.beginTime) }}
          </template>
        </el-table-column>
        <el-table-column
          label="结束时间"
          min-width="120"
          align="center"
          prop="endTime"
        >
          <template v-slot="{ row }">
            {{ parseTime(row.endTime) }}
          </template>
        </el-table-column>
        <el-table-column
          label="预计用纸令数"
          min-width="120"
          align="center"
          prop="expectLing"
        ></el-table-column>
        <el-table-column
          label="实际用纸令数"
          min-width="120"
          align="center"
          prop="realityLing"
        ></el-table-column>

        <!-- <el-table-column
          label="比例（%）"
          min-width="120"
          align="center"
          prop="scale"
        ></el-table-column> -->
        <el-table-column
          label="创建时间"
          width="100"
          align="center"
          prop="createTime"
        >
          <template v-slot="{ row }">
            {{ row.createTime?.split("T")[0] }}
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script setup>
import { getLingCompare } from "@/api/production/cover.js";
const { proxy } = getCurrentInstance();
const loading = ref(true);
const dataList = ref([]);
const router = useRouter();
const total = ref(0);
const hashMap = ref({});

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 50,
  },
});

const getDateNow = () => {
  var myDate = new Date();
  var year = myDate.getFullYear();
  var month = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月，所以要加1);
  var day = myDate.getDate(); //获取当前日（1-31）
  if (month < 10) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  var firstDay = year + "-" + month + "-01";
  return firstDay;
};
const getCurrentDay = () => {
  var now = new Date();
  var year = now.getFullYear();
  var month = now.getMonth() + 1; // 月份从0开始，需要加1
  var day = now.getDate();
  if (month < 10) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  const currentDay = year + "-" + month + "-" + day;
  return currentDay;
};
const dateRange = ref([getDateNow(), getCurrentDay()]);
const { queryParams } = toRefs(data);

const getList = () => {
  loading.value = true;
  getLingCompare({
    ...queryParams.value,
    beginTime: Array.isArray(dateRange.value) ? dateRange.value[0] : undefined,
    endTime:
      Array.isArray(dateRange.value) > 0 ? dateRange.value[1] : undefined,
  })
    .then((res) => {
      loading.value = false;
      dataList.value = res.rows;
      hashMap.value = res.rows[0]?.hashMap;
      total.value = res.total;
    })
    .catch((err) => {
      loading.value = false;
    });
};
// 合计
const getSummaries = (param) => {
  const { columns, data } = param;
  const sums = [];
  columns.forEach((column, index) => {
    if (index === 0) {
      sums[index] = "合计";
      return;
    }
    switch (column.property) {
      case "expectLing":
        sums[index] = hashMap.value.expectLing;
        break;
      case "realityLing":
        sums[index] = hashMap.value.realityLing;
        break;
      default:
        break;
    }
  });
  return sums;
};
// 搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  dateRange.value = [getDateNow(), getCurrentDay()];
  proxy.resetForm("queryRef");
  handleQuery();
};
getList();
</script>

<style lang="scss" scoped>
</style>
